#include "filereversethread.h"
#include <QDir>
#include <QDebug>
#include <windows.h>

FileReverseThread::FileReverseThread():_filepath("")
{
    isStop = false;
}



void FileReverseThread::closeThread()
{
    isStop = true;
}


void FileReverseThread::receiveFilePath(QString filepath){
    _filepath = filepath;
    qDebug() << "filepath:" << _filepath;
}

void FileReverseThread::reverseDir(QString path){
    QDir dir(path);
    if(!dir.exists())
    {
        qDebug() << "it is not true dir_path";
    }
    QFileInfoList info_list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    for(int i= 0 ; i< info_list.count(); i++)
    {
        QFileInfo info = info_list.at(i);
        if(info.isDir())
        {
            QString sub_dir  = info.absoluteFilePath();
            /*递归*/
            reverseDir(sub_dir);
        }else
        {
            QString file_name = info.absoluteFilePath();
            qDebug() << file_name;
            emit sendFilePath(file_name);
            Sleep(5);
        }
    }
}

void FileReverseThread::run()
{
    while (true) {
        if(isStop)
            return;

        if(_filepath == ""){
            Sleep(1);
            continue;
        }
        reverseDir(_filepath);
        isStop = true;
    }
}
